package com.Angaliwa.Whiteboard{
	import com.Angaliwa.Whiteboard;
	
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	
	import mx.controls.TextArea;
	import mx.core.UIComponent;
	
	public class Text {
		public var enabled:Boolean=false,
			   color:String = "000000",
			   thickness:int = 1,
			   whiteboard:Whiteboard;
		
		private var layer:UIComponent,
					isWriting:Boolean=false,
					txt:TextArea;
		
		public function init():void{
			if(txt) endText();
			
			this.layer = whiteboard.getLayer();
			layer.cacheAsBitmap=true;
			
			// To avoid the drawing to 'stick' to the mouse when it leaves
			// the whiteboard, we'll finish the drawing when the mouse
			// leaves the whiteboard.
			layer.parent.addEventListener(MouseEvent.CLICK, click);
		}
		
		public function click(e:MouseEvent):void{
			if(enabled){
				trace(' - ');
				trace(e.target);
				trace(e.target.parent);
				trace(layer);
				trace(' - ');
				
				if(e.target is TextArea && e.target.parent == layer){
					if(isWriting) endText();
					
					trace('Edit existing field..');
					startText(e.target as TextArea);
				} else if(!isWriting){
					trace('Creating new field..');
					startText(undefined, e.localX, e.localY);
				} else if(isWriting){
					trace('Stopping last field..');
					endText();
				}
				
				//if(isWriting && e.currentTarget != txt) endText();
				//	else if(e.currentTarget != txt) startText(e);
				//	else trace('ja, zelfde!');
			}
		}
		
		private function startText(textField:TextArea=undefined, x:int=0, y:int=0, width:int=100, height:int=20):void{
			// DEBUG
			trace('Writing!');
			
			isWriting=true;
			
			if(!textField){
				txt = new TextArea();
				txt.wordWrap = false;
				txt.liveScrolling=false;
				txt.verticalScrollPolicy="off";
				txt.horizontalScrollPolicy="off";
				txt.maxHorizontalScrollPosition=0;
				txt.maxVerticalScrollPosition=0;
				txt.verticalScrollPosition=0;
				txt.horizontalScrollPosition=0;
				txt.minWidth = 20;
				txt.minHeight=25;
				txt.mouseChildren=false;
				txt.name = (Math.random()*100).toString().replace('.', '');
				
				trace('NAME: '+txt.name);
				
				txt.width = width;
				txt.height = height;
				txt.x = x;
				txt.y = y;
				txt.text = "Hello world!";
				
				txt.setStyle("borderThickness", 0);
				txt.setStyle("backgroundAlpha", 0);
				//txt.setStyle("focusAlpha", 0);
				txt.drawFocus(false);
				
				txt.addEventListener(KeyboardEvent.KEY_DOWN, updateTextBox);
				txt.addEventListener(KeyboardEvent.KEY_UP, updateTextBox);
				
				layer.addChild(txt);
			}
			else txt = textField;
			
			txt.editable=true;
			txt.selectable=true;
			txt.buttonMode=false;
			txt.mouseChildren=true;
		}
		
		private function endText():void{
			trace('DONE');
			//txt.editable=false;
			//txt.selectable=false;
			//txt.buttonMode = true;
			//txt.mouseChildren=false;
			
			isWriting=false;
			
			if(whiteboard.settings.get('syncWithServer'))
				whiteboard.features.sync.a(layer.name, {feature:'text',
										thickness:thickness,color:color,
										data:{x:txt.x,y:txt.y,
											  width:txt.width,height:txt.height,
											  text:txt.text,name:txt.name}});
			
			layer.removeChild(txt);
			txt = undefined;
		}
		
		private function updateTextBox(e:KeyboardEvent):void{
			if(isWriting){
				var box:TextArea = (e.target.parent as TextArea);
				
				box.width = Math.ceil(box.textWidth)+15;
				box.height= Math.ceil(box.textHeight)+15;
			}
		}
		
		public function sync(layerName:String,d:Object):void{
			if(whiteboard.settings.get('syncWithServer')){
				var stxt:TextArea = new TextArea();
				stxt.wordWrap = false;
				stxt.liveScrolling=false;
				stxt.verticalScrollPolicy="off";
				stxt.horizontalScrollPolicy="off";
				stxt.maxHorizontalScrollPosition=0;
				stxt.maxVerticalScrollPosition=0;
				stxt.verticalScrollPosition=0;
				stxt.horizontalScrollPosition=0;
				stxt.minWidth = 20;
				stxt.minHeight=25;
				
				stxt.setStyle("borderThickness", 0);
				stxt.setStyle("backgroundAlpha", 0);
				//stxt.setStyle("focusAlpha", 0);
				stxt.drawFocus(false);
				
				stxt.width = d.data.width;
				stxt.height = d.data.height;
				stxt.x = d.data.x;
				stxt.y = d.data.y;
				stxt.text = d.data.text;
				stxt.name = d.data.name;
				
				stxt.editable=false;
				stxt.selectable=false;
				stxt.mouseChildren=false;
				stxt.buttonMode=true;
				
				var tmpLayer:UIComponent = whiteboard.getLayer(layerName),
					itm:TextArea = tmpLayer.getChildByName(d.data.name) as TextArea;
				
				if(itm) tmpLayer.removeChild(itm);
				
				stxt.addEventListener(KeyboardEvent.KEY_DOWN, updateTextBox);
				stxt.addEventListener(KeyboardEvent.KEY_UP, updateTextBox);
				
				tmpLayer.addChild(stxt);
				
				/*
				stxt.alpha=0;
				
				var ani:Fade = new Fade(stxt);
				ani.alphaFrom=0;
				ani.alphaTo=1;
				ani.duration=500;
				ani.easingFunction = Quartic.easeIn;
				ani.play();
				*/
			}
		}
	}
}